package com.pencilisland.pay.mgt.console.init;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pencilisland.pay.mgt.console.bean.MenuData;
import com.pencilisland.pay.mgt.console.entity.SysMenu;
import com.pencilisland.pay.mgt.console.entity.SysOrg;
import com.pencilisland.pay.mgt.console.entity.SysUser;
import com.pencilisland.pay.mgt.console.repository.MenuRepository;
import com.pencilisland.pay.mgt.console.repository.OrgRepository;
import com.pencilisland.pay.mgt.console.repository.UserRepository;

@Component
public class InitBaseData{
	Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	OrgRepository orgRepo;
	@Autowired
	UserRepository userRepo;
	@Autowired
	MenuRepository menuRepo;
	@Autowired
	ObjectMapper objectMapper;

	@PostConstruct
	@Transactional
	public void init() {
		log.info("系统启动初始化...");
		initOrg();
		initUser();
		initMenu();

	}

	public void initOrg() {
		SysOrg org = orgRepo.findOne(1L);
		if (org == null) {
			org = new SysOrg();
			org.setOrgId(1L);
			org.setOrgCode("root");
			org.setOrgNumber("root");
			org.setOrgName("根机构");
			orgRepo.save(org);
		}
		log.info("机构初始化完成");
	}

	public void initUser() {
		if (userRepo.findByLoginName("admin") == null) {
			SysUser user = new SysUser();
			user.setLoginName("admin");
			user.setUserCode("admin");
			user.setUserNumber("admin");
			user.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
			String password = new Md5Hash("888888").toBase64();
			user.setPassword(password);
			user.setOrgId(1L);
			user.setStatus("0");
			user.setUserName("系统后台管理员");
			userRepo.save(user);
		}
		log.info("用户初始化完成");
	}

	public void initMenu() {
		SysMenu menu = menuRepo.findOne(1L);
		if (menu == null) {
			menu = new SysMenu();
			menu.setMenuName("根菜单");
			menu.setMenuDesc("根菜单");
			menuRepo.save(menu);

			// 读取初始化数据
			try {
				File menuDataFile = ResourceUtils.getFile("classpath:init/menu.json");
				List<MenuData> menuData = objectMapper.readValue(menuDataFile, new TypeReference<List<MenuData>>() {
				});
				if (menuData != null && !menuData.isEmpty()) {
					saveMenu(menuData, menu.getMenuId());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		log.info("菜单初始化完成");
	}

	void saveMenu(List<MenuData> menuDatas, Long parent) {
		for (MenuData menuDataTemp : menuDatas) {
			SysMenu menuTemp = new SysMenu();
			menuTemp.setMenuParent(parent);
			menuTemp.setMenuName(menuDataTemp.getMenuName());
			menuTemp.setMenuIcon(menuDataTemp.getMenuIcon());
			menuTemp.setMenuUrl(menuDataTemp.getMenuUrl());
			menuRepo.save(menuTemp);

			if (menuDataTemp.getChilds() != null && !menuDataTemp.getChilds().isEmpty()) {
				saveMenu(menuDataTemp.getChilds(), menuTemp.getMenuId());
			}
		}
	}

}
